import store from '@/vuex/store'
import * as types from '@/vuex/mutation-types'
import * as wsType from './ws-types'
import { webSocketUrl } from '@/util/env.js'
export const createWebsocket = () => {
  let ws = new WebSocket(`${webSocketUrl}\\${store.state.login_user.id}`) 
  ws.onopen = e=>{
    console.log("连接成功了！！！")
  }
  ws.onmessage = e=>{
    console.log("接受到的消息："+e.data)
    const msg = e.data.split(':')
    if(msg[0]=='lineupNo'){//排队
      store.commit(types.RECORD_LINEUP_NUMBER, Number(msg[1]))
    }
  }
  ws.onclose = e => {
    console.log("关闭连接了！！！")
  }
  store.commit(types.RECORD_WEBSOCKET, ws)
}
//排队指令
//参数[0]：用户ID，
//参数[1]：软件ID
export const wsLineup = (...res) => {
  send(wsType.LINE_UP, res)
}
//分配步骤给某个用户
//参数[0]：目标用户ID
//参数[1]：步骤ID
export const wsDistribute = (...res) => {
  send(wsType.DISTRIBUTE, res)
}



//发送websocket到服务端
function send(type, res){
  const msg = `${type}${res.join(':')}`
  store.state.websocket.send(msg)
}